package com.atguigu.flink.chapter11;

import com.atguigu.flink.bean.WaterSensor;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.types.Row;

import static org.apache.flink.table.api.Expressions.$;

public class Flink02_Table_BaseUse_Agg {
       public static void main(String[] args) throws Exception {
               Configuration configuration = new Configuration();
               //web  UI端口
               configuration.setInteger("rest.prot",10000);
               StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
               env.setParallelism(1);

           DataStreamSource<WaterSensor> waterSensorStream =
           env.fromElements(new WaterSensor("sensor_1", 1000L, 10),
                   new WaterSensor("sensor_1", 2000L, 20),
                   new WaterSensor("sensor_2", 3000L, 30),
                   new WaterSensor("sensor_1", 4000L, 40),
                   new WaterSensor("sensor_1", 5000L, 50),
                   new WaterSensor("sensor_2", 6000L, 60));

           // 1. 创建表的执行环境
           StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);
           // 2. 创建表: 将流转换成动态表. 表的字段名从pojo的属性名自动抽取  ,表的列名默认就是属性名
           Table table = tableEnv.fromDataStream(waterSensorStream);
           // 3. 对动态表进行查询  select id, sum(vc) vc_sum from t group by id
          /* Table resultTable = table
                   .groupBy($("id"))
                   .aggregate($("vc").sum().as("vc_sum"))
                   .select($("id"), $("vc_sum"));*/

           //因为不是自定义函数，可以直接在select里调用对应的方法
           Table resultTable = table
                   .groupBy($("id"))
                   .select($("id"), $("vc").sum().as("vc_sum"));

           // 4. 把动态表打印出来
        //   resultTable.execute().print();


          // DataStream<Row> resultStream =
           //  toAppendStream 只能追加不能更新 ！！！
           // tableEnv.toAppendStream(resultTable, Row.class);

           DataStream<Tuple2<Boolean, Row>> retractStream = tableEnv.toRetractStream(resultTable, Row.class);

           retractStream.filter(t->t.f0).map(t->t.f1).print();
           env.execute();

           /*
           因此需要转换成流的话，需要调用

           DataStream<Tuple2<Boolean, Row>> retractStream = tableEnv.toRetractStream(resultTable, Row.class);
           retractStream.print();
           若只看新增
             retractStream.filter(t->t.f0).map(t->t.f1).print();

           * */

           // 流拓扑中没有定义运算符 没有对流进行操作，就不需要进行 env.execute();
           // 不然  IllegalStateException    No operators defined in streaming topology. Cannot execute.

          /* try {
               env.execute();
           } catch (Exception e) {
               e.printStackTrace();
           }*/


       }
}
